/** $Id: NameDocumentListenerUnitTest.java 63 2010-02-14 13:52:34Z aaron.day $ */
package org.crazydays.gameplan.map.swing;


import java.lang.reflect.Method;

import javax.swing.event.DocumentEvent;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import org.junit.*;
import static org.junit.Assert.*;

import static org.easymock.EasyMock.*;
import static org.easymock.classextension.EasyMock.*;

import org.crazydays.gameplan.map.GameMap;

/**
 * NameDocumentListenerUnitTest
 */
public class NameDocumentListenerUnitTest
{
    /**
     * Test constructor.
     */
    @Test
    public void testConstructor()
    {
        // objects
        String name = "name";
        GameMap gameMap = new GameMap(name);

        // test
        NameDocumentListener listener = new NameDocumentListener(gameMap);

        // verify
        assertEquals("gameMap", gameMap, listener.gameMap);
    }

    /**
     * Test changedUpdate(DocumentEvent).
     * 
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    @Test
    public void testChangedUpdate()
        throws SecurityException, NoSuchMethodException
    {
        // mocks
        NameDocumentListener listener =
            createMock(NameDocumentListener.class,
                new Method[] { NameDocumentListener.class.getDeclaredMethod(
                    "handleEvent", DocumentEvent.class) });
        DocumentEvent event = createMock(DocumentEvent.class);

        // expect
        listener.handleEvent(event);

        // replay
        replay(listener, event);

        // test
        listener.changedUpdate(event);

        // verify
        verify(listener, event);
    }

    /**
     * Test insertUpdate(DocumentEvent).
     * 
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    @Test
    public void testInsertUpdate()
        throws SecurityException, NoSuchMethodException
    {
        // mocks
        NameDocumentListener listener =
            createMock(NameDocumentListener.class,
                new Method[] { NameDocumentListener.class.getDeclaredMethod(
                    "handleEvent", DocumentEvent.class) });
        DocumentEvent event = createMock(DocumentEvent.class);

        // expect
        listener.handleEvent(event);

        // replay
        replay(listener, event);

        // test
        listener.insertUpdate(event);

        // verify
        verify(listener, event);
    }

    /**
     * Test removeUpdate(DocumentEvent).
     * 
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    @Test
    public void testRemoveUpdate()
        throws SecurityException, NoSuchMethodException
    {
        // mocks
        NameDocumentListener listener =
            createMock(NameDocumentListener.class,
                new Method[] { NameDocumentListener.class.getDeclaredMethod(
                    "handleEvent", DocumentEvent.class) });
        DocumentEvent event = createMock(DocumentEvent.class);

        // expect
        listener.removeUpdate(event);

        // replay
        replay(listener, event);

        // test
        listener.changedUpdate(event);

        // verify
        verify(listener, event);
    }

    /**
     * Test handleEvent(DocumentEvent).
     * 
     * @throws BadLocationException
     */
    @Test
    public void testHandleEvent()
        throws BadLocationException
    {
        // objects
        String name = "name";
        String updated = "updated";
        GameMap gameMap = new GameMap(name);

        // mocks
        DocumentEvent event = createMock(DocumentEvent.class);
        Document document = createMock(Document.class);

        // setup
        NameDocumentListener listener = new NameDocumentListener(gameMap);

        // expect
        expect(event.getDocument()).andReturn(document);
        expect(document.getLength()).andReturn(updated.length());
        expect(document.getText(0, updated.length())).andReturn(updated);

        // replay
        replay(event, document);

        // test
        listener.handleEvent(event);

        // verify
        verify(event, document);

        assertEquals("name", updated, gameMap.getName());
    }
}
